// 云函数入口文件
const userDelById = require('./userdel/index');
const userAdd = require('./useradd/index');
const cloud = require('wx-server-sdk')

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
}) // 使用当前云环境

const db = cloud.database(); //引入数据库

// 云函数入口函数
exports.main = async (event, context) => {
    // const wxContext = cloud.getWXContext()

    // return {
    //     event,
    //     openid: wxContext.OPENID,
    //     appid: wxContext.APPID,
    //     unionid: wxContext.UNIONID,
    // }
    //  const wxContext = cloud.getWXContext();//目的：获取_openid
    //event获取参数
    const wxContext = cloud.getWXContext();

    switch (event.type) {
        case "del":
            return await userDelById.main(event, context);
        case  "add":
            return await userAdd.main(event, context);
        default:
            return await db.collection("userinfos").where({
                _openid:wxContext.OPENID
            }).get();

    }

}